package com.onlyphoto.onlymall.mvp.view.activity

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPager
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.utils.Constants
import com.onlyphoto.onlymall.utils.PermissionUtil
import com.onlyphoto.onlymall.utils.SharePreferenceUtil
import kotlinx.android.synthetic.main.activity_guide.*
import qiu.niorgai.StatusBarCompat

class GuideActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_guide)
        StatusBarCompat.translucentStatusBar(this, true)
        initListener()
        PermissionUtil.requestPermission(this)
    }

    internal var isTransparent = false
    private var curPosition: Int = 0
    internal var images = intArrayOf(R.mipmap.guide1, R.mipmap.guide2, R.mipmap.guide3)

    fun initListener() {

        viewpager.adapter = GuidePagerAdapter()
        viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {

            override fun onPageSelected(arg0: Int) {
                curPosition = arg0
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
                if (position == images.size && positionOffset == 0f
                        && positionOffsetPixels == 0) {

                    if (isTransparent) {

                        SharePreferenceUtil.getInstance(this@GuideActivity).setIntData(Constants.FIRST_IN, Constants.NOT_FIRST_IN)

                        //获取intent对象
                        intent.setClass(this@GuideActivity, MainActivity::class.java)
                        // 获取class是使用::反射
                        startActivity(intent)

                        finish()
                        isTransparent = false
                    }
                }
            }

            override fun onPageScrollStateChanged(position: Int) {
                isTransparent = position == ViewPager.SCROLL_STATE_DRAGGING && curPosition == images.size
            }
        })

    }

    private inner class GuidePagerAdapter internal constructor() : PagerAdapter() {

        internal var views = arrayOfNulls<ImageView>(images.size)

        private fun getImageView(i: Int): ImageView {
            val view = ImageView(this@GuideActivity)
            val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
            view.layoutParams = params
            view.scaleType = ImageView.ScaleType.FIT_XY
            view.setImageResource(images[i])
            if (i == images.size - 1) {
                view.setOnClickListener {
                    SharePreferenceUtil.getInstance(this@GuideActivity).setIntData(Constants.FIRST_IN, Constants.NOT_FIRST_IN)

                    //获取intent对象
                    intent.setClass(this@GuideActivity, MainActivity::class.java)
                    // 获取class是使用::反射
                    startActivity(intent)

                    finish()
                }
            }
            return view
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val view = getImageView(position)
            container.addView(view)
            views[position] = view
            return view
        }

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(views[position])
            views[position] = null
        }

        override fun getCount(): Int {
            return images.size
        }

        override fun isViewFromObject(arg0: View, arg1: Any): Boolean {
            return arg0 === arg1
        }

    }

}
